/**
 * @fileOverview hover node flowing orbit
 * @author huangtonger@aliyun.com
 */
const Page = require('../../base');
const Global = require('../global');
const Util = require('../util/');

Page.registerBehaviour('orbit', diagram => {
  const graph = diagram.getGraph();
  let hoverItem;
  // let hideOrbitAble = true;
  graph.behaviourOn('beforepanitem', () => {
    diagram.hideOrbit();
  });
  graph.behaviourOn('node:mouseenter', ({ item }) => {
    if (diagram.getSignal('panningItem') || diagram.getSignal('dragEdge')) {
      return;
    }
    hoverItem = item;
    diagram.showOrbit(item);
  });
  graph.on('beforeviewportchange', () => {
    diagram.hideOrbit();
  });
  graph.behaviourOn('mousemove', ({ item, x, y }) => {
    if (!hoverItem) { return; }
    const box = hoverItem.getBBox();
    const distance = Util.euclideanDistance.pointPoint({ x: box.centerX, y: box.centerY }, { x, y });
    const matrix = graph.getMatrix();
    const scale = matrix[0];
    // 如果鼠标位置大于
    if (item !== hoverItem && distance > (box.width / 2 + Global.orbitGap / scale)) {
      diagram.hideOrbit();
      resetStatus();
    }
    if (hoverItem) {
      diagram.layoutOrbit(hoverItem, { x, y });
    }
  });
  function resetStatus() {
    hoverItem = undefined;
  }
});
